'use strict';
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import {exec} from 'child_process';
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

    // Use the console to output diagnostic information (console.log) and errors (console.error)
    // This line of code will only be executed once when your extension is activated
    // console.log(vscode.workspace.getConfiguration('save').get('build'))
    const buildPath:{[key:string]:string}|undefined = vscode.workspace.getConfiguration('save').get('build');
    // const buildPath:{[key:string]:string}|undefined = {
    //     "z:\\web\\weixin\\tas-front-share":"\\build\\build.js"
    // };
    if(buildPath==null){
        return;
    }
    const debugFlag:boolean|undefined = vscode.workspace.getConfiguration('save').get('debugger');
    const run:{[key:string]:string} = {};
    Object.keys(buildPath).forEach((key:string)=>{
        run[key.toLowerCase()] = key.toLowerCase()+buildPath[key];
    })
    vscode.window.showInformationMessage(vscode.workspace.name+'启动文件侦听');
    
    // console.log(vscode.workspace.getConfiguration("node").get('path'));
    // The command has been defined in the package.json file
    // Now provide the implementation of the command with  registerCommand
    // The commandId parameter must match the command field in package.json
    // let disposable = vscode.commands.registerCommand('extension.sayHello', () => {
    //     // The code you place here will be executed every time your command is executed
    //     console.log(vscode.workspace.getConfiguration("node").get('path'));
        
    //     // Display a message box to the user
    //     vscode.window.showInformationMessage('Hello World!');
    // });
    // context.subscriptions.push(disposable);
    // context.subscriptions(vscode.TextEdit.)
    // var extension = new RunOnSaveExtension(context);
    if(debugFlag){
        vscode.window.showInformationMessage('配置信息',JSON.stringify(run));
    }
    vscode.workspace.onDidSaveTextDocument((document: vscode.TextDocument) => {
        if(debugFlag){
            vscode.window.showInformationMessage('文件保存：'+document.fileName);
        }
        for(let tmp in run){
            if(document.fileName.startsWith(tmp)){
                if(debugFlag){
                    vscode.window.showInformationMessage(`文件执行命令：node ${run[tmp]} ${document.fileName}`);
                }
                exec(`node ${run[tmp]} ${document.fileName}`,(error, stdout, stderr)=>{
                    if (error) {
                        vscode.window.showInformationMessage(`命令执行失败: ${error}`);
                        return;
                    }
                    if(debugFlag){
                        vscode.window.showInformationMessage(`执行命令成功，输出成果：${stdout}`);
                    }
                    if(/success:true/.test(stdout)){
                        vscode.window.showInformationMessage(`${document.fileName}文件,命令执行成功`);
                    }
                });
            }
        }
	});
}

// this method is called when your extension is deactivated
export function deactivate() {
}